package com.example.spring_forum.services.impl;

import com.example.spring_forum.common.AppResult;
import com.example.spring_forum.common.ResultCode;
import com.example.spring_forum.dao.ArticleMapper;
import com.example.spring_forum.exception.ApplicationException;
import com.example.spring_forum.exception.model.Article;
import com.example.spring_forum.services.IArticleService;
import com.example.spring_forum.services.IBoardService;
import com.example.spring_forum.services.IUserService;
import com.example.spring_forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

     @Resource
     private ArticleMapper articleMapper;
     @Resource
     private IUserService userService;
     @Resource
     private IBoardService boardService;

    @Override
    public List<Article> selectAll() {
        //调用dao
        List<Article> result=articleMapper.selectAll();
        //返回结果
        return result;
    }


  @Override
    public List<Article> selectByBoardId(Long boardId) {

        if(boardId == null || boardId<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));

        }
        //调用dao

        List<Article> result=articleMapper.selectByBoardId(boardId);

        return result;
    }

    @Override
    public void create(Article article) {
        //非空校验

        if(article == null || article.getBoardId() == null
                || article.getBoardId()<=0
                || article.getUserId() ==null
                || article.getUserId()<=0
                || StringUtils.isEmpty(article.getTitle())
                || StringUtils.isEmpty(article.getContent())
        ){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));

        }
         //设置默认值

        article.setVisitCount(0); //访问数量
        article.setReplyCount(0); //回复数量
        article.setLikeCount(0); //点赞数量
        article.setState((byte)0); //状态
        article.setDeleteState((byte)0); //是否删除
        Date date=new Date();
        article.setCreateTime(new Date()); //创建时间
        article.setUpdateTime(new Date()); //更新时间

        int articleRow = articleMapper.insertSelective(article);
        if (articleRow != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED_CREATE.toString() + " 新增帖子失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

        // 更新用户的帖子数
        userService.addOneArticleCountById(article.getUserId());
        // 更新版块的帖子数
        boardService.addOneArticleCountById(article.getBoardId());

     //   throw new ApplicationException("测试抛出异常，查看数据库是否写入了数据");
        // 打印日志
       log.info(ResultCode.SUCCESS.toString() + " 帖子发布成功, articleId = " + article.getId());
    }

    @Override
    public Article selectById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        Article article = articleMapper.selectById(id);
        // 返回结果
        return article;
    }

    @Override
    public void updateVisitCount(Long id) {
        if(id == null || id<=0){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        int row=articleMapper.updateVisitCount(id);
        if(row!=1){
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString() + "更新访问次数失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void modify(Long id, String title, String content) {
        if(id == null || id<=0 || StringUtils.isEmpty(title) || StringUtils.isEmpty(content)){
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString() );
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构造更新对象
        Article article=new Article();
        article.setId(id);//帖子id
        article.setTitle(title);//标题
        article.setContent(content);//内容
        //更新时间
        Date date=new Date();
        article.setUpdateTime(date);

        int row=articleMapper.updateByPrimaryKeySelective(article);

        if(row!=1){
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString()+"修改帖子失败");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));

        }
    }

    @Override
    public void updateById(Article article) {
        if (article == null || article.getId() == null || article.getId() <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(article);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }


    @Override
    public List<Article> selectByUserId(Long userId) {
        // 非空校验
        if (userId == null || userId <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO
        List<Article> result = articleMapper.selectByUserId(userId);
        // 返回结果
        return result;
    }
}
